import React from 'react'
// ===================================================================== Temp
import SearchForm from '#cpt/temp/SearchForm'
import Table from '#cpt/temp/Table'
import Controls from '#cpt/temp/Controls'
import TableControls from '#cpt/temp/TableControls'
import AddEditModal from '#cpt/temp/AddEditModal'
// ===================================================================== global declare
const { $fn, $Lazy } = window
// ===================================================================== Lazy
const $cache = $Lazy.file(()=>import('#private/cache-api'))
// ===================================================================== component
class Index extends React.Component{
	state = {
		data:[],
		pag: {},
		selectedKeys:[],
		// 搜索表单列表
		forms : [
			{ label: '科室',		name: 'project_pid', 	type: 'select', loading:true,	idStr:'uuid' },
			{ label: '物流人员',		name: 'empl_id', 		type: 'select', loading:true, },
		],
		// 提交表单列表
		formData : [
			{ label: '科室',		name: 'project_pid', 	type: 'select', loading:true,	idStr:'uuid', half:true },
			{ label: '物流人员',		name: 'empl_id',		type: 'select', loading:true, half:true },
		],
	}
	
	model = {}
	
	componentDidMount(){
		this.props.onRef?.(this)
		this.getSelect()
		if(window.isInit){
			this.fetch()
		}
	}
	// 获取下拉数据
	getSelect = () => {
		const { forms, formData } = this.state
		$cache.then( async f => {
			// 科室
			$fn.setRows( [ forms, formData ], 'project_pid', await f.getCache( { api:'ke_shi',param:{type:2}, refresh:true } ))
			// 设备状态
			$fn.setRows( [ forms, formData ], 'empl_id', await f.getCache( { api:'employee' } ))
			this.setState({ forms })
		})
	}
	// paging
	fetch = () => $fn.fetch.call(this,'specimen/outsourcingEmployeeList')
	
	// 表单
	cols = [
		{ title: '科室', 		dataIndex: 'project_name', 	width:200 },
		{ title: '物流人员', 	dataIndex: 'real_name', 	...$fn.table.name },
		TableControls.controls.call(this)
	]
	// 表格操作按钮
	table_controls = (text,rows) => {
		return [
			TableControls.edit.call(this, null, null,{
				select: this.getSelect,
				rows,
				click: ()=>{$fn.disabledRows(this.state.formData, ['project_pid'], true)}
			}),																			// 编辑
		]
	}
	// 操作按钮
	controls = () => [
		Controls.add.call(this,  { label:'关联物流人员', click: ()=>{
			const { formData } = this.state
			this.getSelect()
			$fn.disabledRows(this.state.formData, ['project_pid'], false)
			this.setState({ formData })
		} } )
	]
	
	render(){
		return (
			<>
				{/* 搜索 */}
				{ SearchForm.call(this) }
				{/* 表格 */}
				{ Table.call(this,{isInit:window.isInit}) }
				{/* 添加、编辑 */}
				{ AddEditModal.call(this,{ addText:'关联物流人员', api: 'specimen/addOutsourcingEmployee', editApi:'specimen/editOutsourcingEmployee' }) }
			</>
		)
	}
}

export default Index